﻿using System;
using System.Collections.Generic;
using System.Linq;
using Edu.Web.Data.DAO;
using Edu.Web.Data.DV;
using Edu.Web.Data.Entity;
using ESoft.Core.Data.Service.Impl;
using ESoft.Core.Utils.Extensions;

namespace Edu.Web.Data.Service.Impl
{
    public class UserService : BaseService<UserEntity, long>, IUserService
    {
        private readonly ITimeTableProjectDAO _tpDao;
        private readonly ITeacherDAO _teacherDao;

        public UserService(IUserDAO dao, ITimeTableProjectDAO tpDao, ITeacherDAO teacherDao) : base(dao)
        {
            _tpDao = tpDao;
            _teacherDao = teacherDao;
        }

        public List<TeacherDV> GetTeacherListByCourceId(long courceId)
        {
            var teacherIdList = _tpDao.LoadEntities(x => x.CourceId == courceId).Select(x => x.UserId).ToList();
            return _teacherDao.GetTeacherList().Where(x => teacherIdList.Contains(x.Id)).ToList();
        }

        public long RegisterUser(UserEntity user)
        {
            if (this._baseDao.Exist(x => x.UserName.Equals(user.UserName)))
            {
                throw new Exception("存在相同的用户名！");
            }
            user.CreateTime = DateTime.Now;
            user = this.Add(user);
            return user.Id;
        }
    }
}